//
//  GroupManagerViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "GroupManagerViewController.h"
#import "SystemSetingCell.h"
#import "TransferGroupOwnerViewController.h"
@interface GroupManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSArray *dataArr;
@end

@implementation GroupManagerViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.navTitleLabel.text = @"群管理";
    [self setUpView];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.right.left.bottom.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    NSNumber *switchStatus = @0;
    switch (self.tIMGroupAddOpt) {
        case TIM_GROUP_ADD_ANY:
            switchStatus = @0;
            break;
        case TIM_GROUP_ADD_AUTH:
            switchStatus = @1;
            break;
        default:
            break;
    }
    self.dataArr = @[@[@{@"title":@"群聊邀请确认",@"detail":switchStatus}],
                     @[@{@"title":@"群主管理权转让",@"detail":@""}]];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    } else if (section == 1) {
        return 48;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
        UILabel *tipsLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH, 18)];
        tipsLab.text = @"启用后，群成员需要群主确认才能邀请朋友进群。";
        tipsLab.textColor = [UIColor colorWithHexString:@"#B0B0B0"];
        tipsLab.font = [UIFont systemFontOfSize:13];
        [headerView addSubview:tipsLab];
        return headerView;
    } else {
        return [[UIView alloc] init];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemSetingCell *cell = [SystemSetingCell cellWithTableView:tableView];
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    [cell configGroupManagerCellWithDataDic:self.dataArr[indexPath.section][indexPath.row] indexPath:indexPath];
    
    //群邀请确认开关
    @weakify(self);
    [cell setMySwitchBlock:^(BOOL switchStuts) {
        @strongify(self);
        [self groupInviteSetUpWithSwitchStatus:switchStuts];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        TransferGroupOwnerViewController *vc = [[TransferGroupOwnerViewController alloc] init];
        vc.groupMemberArr = self.groupMemberArr;
        vc.groupId = self.groupId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - target action

#pragma mark - private method
- (void)groupInviteSetUpWithSwitchStatus:(BOOL)status {
    if (status) {
        //开启状态
        @weakify(self);
        [[TIMGroupManager sharedInstance] modifyGroupAddOpt:self.groupId opt:TIM_GROUP_ADD_AUTH succ:^{
            @strongify(self);
            [self toastWithContent:@"已开启"];
        } fail:^(int code, NSString *msg) {
            @strongify(self);
            [self toastWithContent:msg];
        }];
    } else {
        //关闭状态
        @weakify(self);
        [[TIMGroupManager sharedInstance] modifyGroupAddOpt:self.groupId opt:TIM_GROUP_ADD_ANY succ:^{
            @strongify(self);
            [self toastWithContent:@"已关闭"];
        } fail:^(int code, NSString *msg) {
            @strongify(self);
            [self toastWithContent:msg];
        }];
    }
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 56;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
@end
